﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;

    public static class QuickSorter<T> where T: IComparable
    {
        public static void Sort(IList<T> list, bool isAsc)
        {
            QuickSorter<T>.Sort(list, 0, list.Count - 1, isAsc);
        }

        private static void Sort(IList<T> list, int left, int right, bool isAsc)
        {
            if (left < right)
            {
                T local;
                int x = left;
                int y = right + 1;
                if (!isAsc)
                {
                    while (x < y)
                    {
                        x++;
                        local = list[left];
                    Label_009A:
                        if (x < right)
                        {
                            T local4 = list[x];
                            if (local4.CompareTo(local) > 0)
                            {
                                x++;
                                goto Label_009A;
                            }
                        }
                        y--;
                    Label_00C7:
                        if (y >= left)
                        {
                            T local5 = list[y];
                            if (local5.CompareTo(local) < 0)
                            {
                                y--;
                                goto Label_00C7;
                            }
                        }
                        if (x < y)
                        {
                            list.Swap<T>(x, y);
                        }
                    }
                }
                else
                {
                    while (x < y)
                    {
                        x++;
                        local = list[left];
                        goto Label_0027;
                    Label_0023:
                        x++;
                    Label_0027:
                        if (x < right)
                        {
                            T local2 = list[x];
                            if (local2.CompareTo(local) < 0)
                            {
                                goto Label_0023;
                            }
                        }
                        y--;
                        goto Label_0053;
                    Label_004F:
                        y--;
                    Label_0053:
                        if (y >= left)
                        {
                            T local3 = list[y];
                            if (local3.CompareTo(local) > 0)
                            {
                                goto Label_004F;
                            }
                        }
                        if (x < y)
                        {
                            list.Swap<T>(x, y);
                        }
                    }
                }
                list.Swap<T>(left, y);
                QuickSorter<T>.Sort(list, left, y - 1, isAsc);
                QuickSorter<T>.Sort(list, y + 1, right, isAsc);
            }
        }
    }
}

